package com.lzx.iteam.net;

import android.os.Message;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzx.iteam.bean.SalaryGroupBean;
import com.lzx.iteam.bean.SalaryListBean;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by xuepengju on 14-3-20.
 */
public class GetSalaryGroupsMsg extends CcMsgStructure {

    private Message mCallback;

    public GetSalaryGroupsMsg(Message callback){
        mCallback = callback;
    }

    @Override
    public void onReceive(JSONObject data) {
        ArrayList<SalaryGroupBean> mGroups = new ArrayList<>();
        try {
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            mGroups = new Gson().fromJson(jsonObject.getString("groups"),
                  new TypeToken<ArrayList<SalaryGroupBean>>() {
                  }.getType());
            JSONArray array = jsonObject.getJSONArray("groups");
            for (int i = 0; i < array.length(); i++) {
                JSONObject obj = array.getJSONObject(i).getJSONObject("group_rule");

                /**
                 * 当 值为空时表示没有设置，这里暂且置为-1，
                 * 获取数据时判断 -1就取默认值
                 */
                if (!obj.has("p_pension_pc")){
                    mGroups.get(i).getGroup_rule().setP_pension_pc(-1);
                }
                if (!obj.has("p_medical_pc")){
                    mGroups.get(i).getGroup_rule().setP_medical_pc(-1);
                }
                if (!obj.has("p_medical_count")){
                    mGroups.get(i).getGroup_rule().setP_medical_count(-1);
                }
                if (!obj.has("p_provident_fund_pc")){
                    mGroups.get(i).getGroup_rule().setP_provident_fund_pc(-1);
                }
                if (!obj.has("p_unemployment_pc")){
                    mGroups.get(i).getGroup_rule().setP_unemployment_pc(-1);
                }


                if (!obj.has("c_pension_pc")){
                    mGroups.get(i).getGroup_rule().setC_pension_pc(-1);
                }
                if (!obj.has("c_medical_pc")){
                    mGroups.get(i).getGroup_rule().setC_medical_pc(-1);
                }
                if (!obj.has("c_provident_fund_pc")){
                    mGroups.get(i).getGroup_rule().setC_provident_fund_pc(-1);
                }
                if (!obj.has("c_unemployment_pc")){
                    mGroups.get(i).getGroup_rule().setC_unemployment_pc(-1);
                }
                if (!obj.has("c_injury_pc")){
                    mGroups.get(i).getGroup_rule().setC_injury_pc(-1);
                }
                if (!obj.has("c_birth_pc")){
                    mGroups.get(i).getGroup_rule().setC_birth_pc(-1);
                }
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (mCallback != null)
        {
            mCallback.obj = mGroups;
            mCallback.arg1 = 0;
            mCallback.sendToTarget();
            return;
        }
    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if(mCallback != null){
            mCallback.arg1 = error;
            if(!StringUtil.isEmpty(errorMsg)){
                mCallback.obj = errorMsg;
            }else{
                mCallback.obj = Constants.NETWORK_WRROR;
            }
            mCallback.sendToTarget();
        }
    }
}
